import React from 'react'
import PropTypes from 'prop-types'
import { Table, Modal } from 'antd'
import styles from './List.less'
import classnames from 'classnames'
import AnimTableBody from '../../components/DataTable/AnimTableBody'
import { DropOption } from 'components'
import { Link } from 'dva/router'

const confirm = Modal.confirm

const List = ({ onDeleteItem, onEditItem, isMotion, location, ...tableProps }) => {
  const handleMenuClick = (record, e) => {
    if (e.key === '1') {
      onEditItem(record)
    } else if (e.key === '2') {
      confirm({
        title: '确定删除该模块?',
        onOk () {
          console.log(record)
          onDeleteItem(record.pk)
        },
      })
    }
  }
  const {productList}=tableProps
  let productName=function(id){
    for(let i=0;i<productList.length;i++){
      if(productList[i].pk==id){
        return productList[i].fields.name
      }
    }
  }
  const columns = [
    {
      title: '模块ID',
      dataIndex: 'pk',
      key: 'pk',
      width: 80,
      // render: (text, record) => <Link to={`user/${record.id}`}>{text}</Link>,
    },{
      title: '模块名称',
      dataIndex: 'fields.name',
      key: 'name',
      className:'nameCol',
      // width: 64,
      // className: styles.avatar,
      render: (text,record) => record.fields.name,
    },{
      title: '产品名称',
      dataIndex: 'fields.product_name',
      key: 'product_name',
      className:'nameCol',
      // width: 64,
      // className: styles.avatar,
      render: (text,record) => productName(record.fields.product_name),
    }
    , {
      title: '操作',
      key: 'operation',
      width: 100,
      render: (text, record) => {
        return <DropOption onMenuClick={e => handleMenuClick(record, e)} menuOptions={[{ key: '1', name: '修改' }, { key: '2', name: '删除' }]} />
      },
    },
  ]

  const getBodyWrapperProps = {
    page: location.query.page,
    current: tableProps.pagination.current,
  }

  const getBodyWrapper = body => { return isMotion ? <AnimTableBody {...getBodyWrapperProps} body={body} /> : body }

  return (
    <div>
      <Table
        {...tableProps}
        className={classnames({ [styles.table]: true, [styles.motion]: isMotion })}
        bordered
        columns={columns}
        simple
        rowKey={record => record.pk}
        getBodyWrapper={getBodyWrapper}
        rowSelection={null}
      />
    </div>
  )
}

List.propTypes = {
  onDeleteItem: PropTypes.func,
  onEditItem: PropTypes.func,
  isMotion: PropTypes.bool,
  location: PropTypes.object,
}

export default List
